package com.maimao.info.service;

import com.google.common.collect.Lists;
import com.maimao.core.constant.ListData;
import com.maimao.core.exception.MaiMaoDaoException;
import com.maimao.core.model.resp.Result;
import com.maimao.info.client.GoodsClient;
import com.maimao.info.mapper.CollectMapper;
import com.maimao.model.info.Collect;
import com.maimao.model.info.enums.FollowType;
import com.maimao.model.info.vo.FollowGoods;
import com.maimao.model.user.Foot;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author MaoLin Wang
 * @date 2021/1/11 5:28 下午
 */
@Service
public class CollectionService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private GoodsClient goodsClient;

    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void addCollectionBatch(List<Collect> collectList) {
        collectList= collectList.stream()
                .filter(collect ->
                        collectMapper.selectByUserIdAndTidAndType(collect.getUserId(), collect.getTargetId(), collect.getType()) ==null)
                .collect(Collectors.toList());

        if (collectList.size() > 0) {
            collectMapper.insertBatch(collectList);
        }

    }

    public List<FollowGoods> followGoodsList(Long userId) {
        List<Collect> collects = collectMapper.selectByUserIdAndType(userId, FollowType.GOODS.getStatus());
        if (ObjectUtils.isNotEmpty(collects)) {
            Result<List<FollowGoods>> res = goodsClient.followGoods(collects);
            if (res != null && res.getData() != null) {
                return res.getData();
            }
        }
        return ListData.EMPTY_LIST;
    }

    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void remove(Long id) {
        collectMapper.remove(id);
    }
}
